package org.mozhu.mboot.core.web.freemarker;

import com.google.common.collect.Lists;
import freemarker.ext.jsp.TaglibFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.List;

/**
 * 将常用的静态工具类暴露到freemarker里使用
 */
@Configuration
@Slf4j
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
public class FreeMarkerSecurityAutoConfiguration {

    @Autowired
    public void freeMarkerConfigurer(FreeMarkerConfigurer configurer) {
        List<String> tlds = Lists.newArrayList("/tags/security.tld");
        TaglibFactory taglibFactory = configurer.getTaglibFactory();
        taglibFactory.setClasspathTlds(tlds);
        if (taglibFactory.getObjectWrapper() == null) {
            taglibFactory.setObjectWrapper(configurer.getConfiguration().getObjectWrapper());
        }
    }

}
